package cn.edu.cqvie.jdk8.fun;

import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class PersonTest {

    public static void main(String[] args) {
        Person person1 = new Person("zhangsan", 20);
        Person person2 = new Person("lishi", 30);
        Person person3 = new Person("wangwu", 40);

        List<Person> persons = Arrays.asList(person1, person2, person3);
        PersonTest test = new PersonTest();
        List<Person> list = test.getPersonsByUsername("zhangsan", persons);
        list.forEach(System.out::println);

        System.out.println("==================");

        List<Person> list2 = test.getPersonsByAge(20, persons);
        list2.forEach(System.out::println);

        System.out.println("==================");
        List<Person> list3 = test.getPersonsByAge2(20, persons, (age, persionList) ->
            persionList.stream().filter(person -> person.getAge() > age).collect(Collectors.toList()));
        list3.forEach(System.out::println);

        System.out.println("==================");
        List<Person> list4 = test.getPersonsByAge3(new Person("zhangsan", 20), persons, (p, persionList) ->
                persionList.stream().filter(person -> person.getAge() == p.getAge() && person.getUsername().equals(p.getUsername())
                ).collect(Collectors.toList()));
        list4.forEach(System.out::println);

    }

    public List<Person> getPersonsByUsername(String username, List<Person> persons) {
        return persons.stream().filter(person -> person.getUsername().equals(username))
                .collect(Collectors.toList());
    }

    public List<Person> getPersonsByAge(int age, List<Person> persons) {
        BiFunction<Integer, List<Person> ,List<Person>> biFunction = (ageOfPerson, personList) ->
                personList.stream().filter(person -> person.getAge() > age).collect(Collectors.toList());
        return biFunction.apply(age, persons);
    }


    public List<Person> getPersonsByAge2(int age, List<Person> persons, BiFunction<Integer, List<Person>, List<Person>> biFunction) {
        return biFunction.apply(age, persons);
    }

    public List<Person> getPersonsByAge3(Person person, List<Person> persons, BiFunction<Person, List<Person>, List<Person>> biFunction) {
        return biFunction.apply(person, persons);
    }
}
